<?php
class GoodsListWidget extends YWidget{
	public $count = 5;
	public $title = '商品列表';
	public $thumb = '50x50';
	public $cate_id = null;
	public $flag = null;
	
	protected $goods_list = null;
	protected $cate = null;
	public function run(){
		$this->load_resource();
		
		$fl = 'name,main_picture,price,specs,market_price';
		$condition = array('site_id' => SITE_ID, 'status'=>GoodsModel::STATUS_ON_SALE);
		if($this->flag != null){
			$condition['goods_flag'] = intval($this->flag);
		}
		
		if($this->cate_id != null){
			if(is_string($this->cate_id)){
				$condition['cate'] = $this->cate_id;
			}elseif(is_array($this->cate_id)){
				$condition['cate'] = array('$in' => $this->cate_id);
			}
			
			$this->cate = GoodsCateModel::model()->get_by_key($this->cate_id);
		}
	
		$mod = new GoodsModel();
		$this->goods_list = $mod->find($condition, $fl, 0, $this->count, array('rank'=>1));
		if(!empty($this->goods_list)){
			$goods_ids = array_keys($this->goods_list);
			$c = new CDbCriteria();
			$c->addInCondition('goods_id', $goods_ids);
			$c->select = 'MIN(price) AS price';
			$c->group = 'goods_id';
			$c->index = 'goods_id';
			
			$goods_spec_list = Goods::model()->findAll($c);
			foreach($goods_spec_list as $goods_id=>$goods){
				if(isset($this->goods_list[$goods_id])){
					//将MySQL中的值赋到数组中
					$this->goods_list[$goods_id]['price'] = $goods->price;
				}
			}
		}
		$this->render($this->class_name . '/' . $this->tpl);
	}
}

?>